//
//  InstallLayerDolphin.h
//  client
//
//  Created by sg on 2/17/17.
//
//

#ifndef InstallLayerDolphin_h
#define InstallLayerDolphin_h



#ifdef USE_GCLOUD
#include "GCloud/GCloudDolphinInterface.h"
#include "GCloud/IGCloud.h"
#include "Tencent/DolphinInfoString.hpp"
#include "UpdateSpeedCounter.hpp"
#endif

#include "cocos2d.h"
#include "editor-support/cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "CCFactory.h"
#include "InstallLayer.h"


USING_NS_CC;

class InstallLayerDolphin : public InstallLayer
#ifdef USE_GCLOUD
,GCloud::GCloudDolphinCallBack
#endif
{
public:
    CREATE_FUNC(InstallLayerDolphin);

    InstallLayerDolphin();

    virtual ~InstallLayerDolphin();

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();

#ifdef USE_GCLOUD
    virtual void update(float);

    void startGrayUpdate(const char * openId, const char * zoneId);
    
    GCloud::dolphinUpdateStage getLastErrorStage() { return _lastErrorStage; }

private:
    //GCloudDolphinCallBack
    void OnDolphinVersionInfo(GCloud::dolphinVersionInfo& newVersinInfo);
    void OnDolphinProgress(GCloud::dolphinUpdateStage curVersionStage, cu_uint64 totalSize, cu_uint64 nowSize);
    void OnDolphinError(GCloud::dolphinUpdateStage curVersionStage, cu_uint32 errorCode);
    void OnDolphinSuccess();
    void OnDolphinNoticeInstallApk(char* apkurl);
    void OnDolphinFirstExtractSuccess();
private:
    void initGCloud();
    void uninitGCloud();
	void startUpdate(GCloud::dolphinUpdateInitType type,bool needFirstExtract);
	void startUpdateProgram();
    void startUpdateResource();
    void continueUpdate();
    void stopUpdate();
	void uninitDolphin();

	void updateSource();
	void updateResource();
    
    std::string getDownSizeShowString(cu_uint64 totalSize, cu_uint64 nowSize);
    
    std::string _dolphinUrl;
    int _dolphinChannelId;
    bool _isStartUpdate;
    bool _waitNewVersion;
    bool _waitMessageBox;
    bool _shouldStop;
    bool _needFirstExtract;
    
    bool _isGrayUpdate;
    const char * _grayOpenId;
    const char * _grayZoneId;
    
    cu_uint32 _lastErrorCode;
    GCloud::dolphinUpdateStage _lastErrorStage;
    GCloud::dolphinUpdateInitType _currentUpdateType;
    GCloud::dolphinUpdateStage _curVersionStage;
    
    UpdateSpeedCounter _speedCounter;
    DolphinInfoString _dolphinInfoString;
    DolphinErrorString _dolphinErrorString;
    GCloud::GCloudDolphinInterface* _dolphin;
    
#endif
    virtual void goStart();
    virtual void goExit();
    virtual void goNext();

private:
    bool _needReloadString;
    std::string _newResourceVersion;
    std::string _iosAppDownloadUrl;
};

#endif
